package com.lemon.smartsafety.ui.activity.safe.data;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.common.EvidenceDataI;
import com.lemon.smartsafety.entity.event.common.HorizontalEvidenceEvent;
import com.lemon.smartsafety.entity.event.datacoll.BuildingPositionRefreshEvent;
import com.lemon.smartsafety.entity.safe.company.BindQrcodeEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingIndexVo;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingItemCountVo;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingVo;
import com.lemon.smartsafety.tag.DefaultCode;
import com.lemon.smartsafetynew.ui.activity.base.BaseFragmentActivity;
import com.lemon.smartsafety.ui.adapter.common.HorizontalEvidenceAdapter;
import com.lemon.smartsafety.ui.fragment.safe.data.BuildingCheckItemListFragment;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.app.ToastUtils;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LemonFileHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafetynew.entity.event.patrol.OneKeyBindEvent;
import com.lemon.smartsafetynew.ui.activity.qrcode.CodeUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 安全巡查 检查项目 一览表
 */
public class BuildingCheckItemListActivity extends BaseFragmentActivity {
    private LoggerHelper loggerHelper = new LoggerHelper(BuildingCheckItemListActivity.class);

    private BuildingCheckItemListFragment mFragment;

    /**
     * bundle info
     */
    private CompanyBuildingVo          mCompanyBuildingVo;
    private CompanyBuildingIndexVo     mCompanyBuildingIndexVo;
    private CompanyBuildingItemCountVo mCompanyBuildingItemCountVo;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            mCompanyBuildingVo = (CompanyBuildingVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILD_INFO);
            mCompanyBuildingIndexVo = (CompanyBuildingIndexVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILDING_INDEX_VO);
            mCompanyBuildingItemCountVo = (CompanyBuildingItemCountVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILDING_ITEM_COUNT);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_fragment_swipe_no_back;
    }

    @Override
    protected void initViews() {
        super.initViews();
    }

    @Override
    public Fragment getFragment() {
        return getFragment(null);
    }


    public Fragment getFragment(String text) {
        Bundle bundle = new Bundle();
        if (null != text) {
            bundle.putString(BundleKey.KEY_WORD, text);
        }
        bundle.putSerializable(BundleKey.COMPANY_BUILD_INFO, mCompanyBuildingVo);
        bundle.putSerializable(BundleKey.COMPANY_BUILDING_ITEM_COUNT, mCompanyBuildingItemCountVo);
        bundle.putSerializable(BundleKey.COMPANY_BUILDING_INDEX_VO, mCompanyBuildingIndexVo);
        mFragment = BuildingCheckItemListFragment.newInstance();
        mFragment.setArguments(bundle);
        return mFragment;
    }


    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.build_check_item_list_title);
    }



    /**
     * 查看照片
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(HorizontalEvidenceEvent event) {
        List<EvidenceDataI> res = event.getData();
        if (res != null && res.size() != 0) {
            EvidenceDataI data = res.get(event.getPosition());
            if (LemonFileHelper.isImageUrl(data.getEvidenceUrl())) {
                List<PhotoInfo> photos = HorizontalEvidenceAdapter.getPhotoInfoList(event.getData());
                if (photos != null && photos.size() != 0) {
                    UIHelper.showPhotoView(this, photos, event.getPosition());
                }
            } else if (LemonFileHelper.isVoiceUrl(data.getEvidenceUrl())) {
                playVoice(HttpUtils.formatFilesUrl(data.getEvidenceUrl()));
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        BuildingPositionRefreshEvent event = new BuildingPositionRefreshEvent();
        event.setTag(BuildingPositionRefreshEvent.class.getName());
        AppService.getBus().post(event);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == DefaultCode.REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    LogUtils.LOGE("解析结果:" +result);
                    //changed by Andrew.yin 2017/9/13 begin, 通过Action判断是否是一键绑定
                    String action = "";
                    if (bundle.containsKey("action")) {
                        action = bundle.getString("action");
                    }
                    if("".equals(action)) {
                        AppService.getBus().post(new BindQrcodeEvent(result, BuildingCheckItemListFragment.class.getName()));
                    } else if(OneKeyBindEvent.class.getName().equals(action)) {
                        AppService.getBus().post(new BindQrcodeEvent(result, OneKeyBindEvent.class.getName()));
                    }
                    //changed by Andrew.yin 2017/9/13 begin
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    ToastUtils.toastMsg("解析二维码失败");
                }
            }
        }

    }
}
